import 'babel-polyfill';
import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
import VueI18n from 'vue-i18n';
import LanguageCN from './i18n/zh-cn';
import LanguageEN from './i18n/zh-en';
import LanguageHK from './i18n/zh-hk';
import store from '@/store';
import BottomNavigation from '@/repeatComponents/bottomNavigation/BottomNavigation';
import Navbar from '@/repeatComponents/navbar/navbar';
import moment from 'moment';
import BottomNav from '@/components/know/BottomNav';
import KnowledgeBottomNav from '@/components/knowledge/KnowledgeBottomNav';
import publicDeploy from '@/components/publicDeploy.js';//共属性，filter,directive
import VeeValidate, { Validator } from 'vee-validate'; //表单校验模块
import VueClipboard from 'vue-clipboard2'; //复制到粘贴板
import preview from 'vue-photo-preview';//图片预览
import vant, { Lazyload } from 'vant';
import 'vue-photo-preview/dist/skin.css';
import 'jquery';
import CommonLoading from '@/components/common/loading';
import '../static/vant.css';
import 'vant/lib/index.css';
import '../static/reset.css';
import '../static/common.css';
import './assets/iconfont.css';
import '@/utils/app-back-button.js';
import wx from '@/components/wechat/JS-SDK-Utils';
import debounceBtn from '@/utils/debounce-btn.js';
import myxss from './libs/xss';
// 实现iview中的alert组件
import vAlert from '@/repeatComponents/v-alert/v-alert';
// echarts是按需引入，在这里引入是因为防止重复加载
import echarts from '@/utils/echarts';
import quillEditor from '@/utils/quill-setup';

// import VConsole from 'vconsole';
// let vConsole = new VConsole();
// Vue.use(vConsole);
Vue.use(Lazyload);
Vue.prototype.$devicePixelRatio = 2;
Vue.config.productionTip = false;
Vue.prototype.$wx = wx;
Vue.prototype.$echarts = echarts;
Vue.prototype.$xss = (val) => {
    return myxss.process(val);
};
if (process.env.NODE_ENV === 'production_app') {
    const baseURL = store.state.config.appUrl;
    let axiosInstance = axios.create({
        baseURL: baseURL // 设置baseurl
    });
    axiosInstance.interceptors.request.use(config => {
        const token = window.localStorage.getItem('token');
        if (token) {
            config.withCredentials = true; // 允许携带token ,这个是解决跨域产生的相关问题
            config.headers = {
                developer: 'mobile',
                'token': token,
                'Content-Type': 'application/json;charset=UTF-8'
            };
        }
        return config;
    }, err => {
        return Promise.reject(err);
    });
    Vue.prototype.$axios = axiosInstance;
} else {
    Vue.prototype.$axios = axios;
}

Vue.prototype.$ajax = axios;
/**
 * @description: 富文本全局过滤器（图片资源加入appurl。）
 * @param {*} textareaHtml
 * @return {*}
 */
Vue.prototype.htmlFilter = function(textareaHtml) {
    if (process.env.NODE_ENV === 'production_app') {
        const baseURL = store.state.config.appUrl;
        var srcReg = /(src="\/([^:]*?)".*?)/ig;
        if (textareaHtml) {
            textareaHtml = textareaHtml.replace(srcReg, "src='" + baseURL + '/' + '$2' + "'");
        }
    }
    return textareaHtml;
};
if (window.localStorage.getItem('globalLang') === 'en') {
    moment.locale('en-us');
} else if (window.localStorage.getItem('globalLang') === 'hk') {
    moment.locale('zh-hk');
} else {
    moment.locale('zh-cn');
}
Vue.prototype.$moment = moment;
// Vue.prototype.$sockBaseUrl = process.env.NODE_ENV !== 'development' ? '' : 'http://192.168.2.202';
getDomainUrl();
function getDomainUrl() {
    axios({
        method: 'GET',
        url: `/app/api/trainee/system-setting/getDomainUrl`
    }).then((res) => {
        let daUrl = res;
        Vue.prototype.$sockBaseUrl = daUrl.data.errMsg || '';
    });
}
Vue.use(debounceBtn);
Vue.use(VueI18n);
Vue.component('BottomNavigation', BottomNavigation);
Vue.component('Navbar', Navbar);
Vue.component('BottomNav', BottomNav);
Vue.component('KnowledgeBottomNav', KnowledgeBottomNav);
Vue.component('CommonLoading', CommonLoading);
Vue.component('quillEditor', quillEditor);
Vue.component('vAlert', vAlert);
Vue.use(publicDeploy, store);
Vue.use(VueClipboard);
Vue.use(preview);
Vue.use(vant);
Vue.prototype.$Validator = Validator;

const i18n = new VueI18n({
    locale: window.localStorage.getItem('globalLang') ? window.localStorage.getItem('globalLang') : 'cn',
    messages: {
        en: LanguageEN,
        cn: LanguageCN,
        hk: LanguageHK
    },
    silentTranslationWarn: true /*「去除warning」-vue项目使用vue-i18n报错 Cannot translate the value of keypath 'xxx'. Use the value of keypath as default.*/

});
Vue.use(VeeValidate, {
    errorBagName: 'errors', // 改变错误信息容器的名称
    fieldsBagName: 'fieldBags',
    i18nRootKey: 'validations',
    i18n,
    dictionary: {
        cn: LanguageCN,
        en: LanguageEN,
        hk: LanguageHK
    }
});

/*eslint-disable no-new*/
new Vue({
    el: '#app',
    router,
    store,
    i18n,
    components: { App },
    template: '<App/>'
});
export default i18n;
